
import { Logger } from "../../engine/Logger";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { UIHelper } from "../util/UIHelper";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ViewJoinRoom extends ViewBase {

  
    private inputNumList: Array<number> = []

    onEnable(): void {
        super.onEnable()
        for (let i = 0; i < 10; i++) {
            let btnNum: cc.Node = this.findChild(this.node, "btnNum" + i)
            UIHelper.onAddEvent(btnNum, () => {
                if (this.inputNumList.length < 6) {
                    this.inputNumList.push(i)
                    this.refresh()
                    if (this.inputNumList.length == 6) {
                        this.gotoJoin()
                    }
                }
            })
        }
        let btnSure: cc.Node = this.findChild(this.node, "btnSure")
        UIHelper.onAddEvent(btnSure, () => {
            // this.inputNumList = []
            // this.refresh()
            this.gotoJoin()
        })
        let btnDel: cc.Node = this.findChild(this.node, "btnDel")
        UIHelper.onAddEvent(btnDel, () => {
            this.inputNumList.pop()
            this.refresh()
        })
        this.refresh()
    }

    private refresh() {
        for (let i = 0; i < 6; i++) {
            let txtNum: cc.Label = this.getChild(this.node, "txtNum" + i, cc.Label)
            if(isNaN(this.inputNumList[i])){
                txtNum.string = ""
            }else{
                txtNum.string = this.inputNumList[i] + ""
            }
        }
    }

    private gotoJoin() {
        let str: string = ""
        for (let i = 0; i < this.inputNumList.length; i++) {
            str += this.inputNumList[i]
        }
        let room_id: number = parseInt(str)
        this.closeView()
        Logger.log("gotoJoin=", room_id)
        // SocketClient.req_join_room(room_id)
    }


}
